/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	RBPromptContent.java
 * 模块说明：	
 * 修改历史：
 * Nov 6, 2007 - lxm - 创建。
 */
package com.hd123.rumba.prompt.provider;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.util.ObjectUtil;
import com.hd123.rumba.util.XMLUtil;

/**
 * rumba系统提醒内容 | 实现类
 * 
 * @author lxm
 * 
 */
public class RBPromptContent extends PromptContent implements IPromptContentWeb {

  /** 数据类型 */
  static enum Type {
    ISNULL(-1), STRING(0), INTEGER(1), BIGDECIMAL(2), DATE(3), BOOLEAN(4);

    private Integer value;

    private Type(int value) {
      this.value = new Integer(value);
    }

    public Integer getValue() {
      return value;
    }
  }

  public String toContext() throws BusinessException {
    Document doc = XMLUtil.newDocument();
    Element root = doc.createElement("promptContent");
    for (String key : this.keySet()) {
      if (key == null)
        continue;
      Object value = this.get(key);
      Element eKey = doc.createElement(key);
      if (value == null)
        XMLUtil.writeInteger(eKey, "type", Type.ISNULL.getValue());
      else {
        if (value instanceof String) {
          XMLUtil.writeInteger(eKey, "type", Type.STRING.getValue());
          XMLUtil.writeString(eKey, "value", (String) value);
        } else if (value instanceof Integer) {
          XMLUtil.writeInteger(eKey, "type", Type.INTEGER.getValue());
          XMLUtil.writeInteger(eKey, "value", (Integer) value);
        } else if (value instanceof BigDecimal) {
          XMLUtil.writeInteger(eKey, "type", Type.BIGDECIMAL.getValue());
          XMLUtil.writeBigDecimal(eKey, "value", (BigDecimal) value);
        } else if (value instanceof Date) {
          XMLUtil.writeInteger(eKey, "type", Type.DATE.getValue());
          XMLUtil.writeDate(eKey, "value", (Date) value);
        } else if (value instanceof Boolean) {
          XMLUtil.writeInteger(eKey, "type", Type.BOOLEAN.getValue());
          XMLUtil.writeBoolean(eKey, "value", (Boolean) value);
        }
      }
      root.appendChild(eKey);
    }
    doc.appendChild(root);
    return XMLUtil.transform(doc);
  }

  public void applyParams(Map<String, Object> dict) throws BusinessException {
    // TODO Auto-generated method stub

  }

  public boolean sameAs(IPromptContent target) throws BusinessException {
    if (!super.sameAs(target))
      return false;
    if (!(target instanceof RBPromptContent))
      return true;
    return ObjectUtil.equals(this.getLink(), ((RBPromptContent) target).getLink())
        && ObjectUtil.equals(this.getLinkPerm(), ((RBPromptContent) target).getLinkPerm());
  }

  public String getLink() {
    return (String) this.get("link");
  }

  public void setLink(String link) {
    if (link == null)
      this.remove("link");
    else
      this.put("link", link);
  }

  public String getLinkPerm() {
    return (String) this.get("linkPerm");
  }

  public void setLinkPerm(String linkPerm) {
    if (linkPerm == null)
      this.remove("linkPerm");
    else
      this.put("linkPerm", linkPerm);
  }

}
